package com.zuo.demo01.controller.admin;


import com.alibaba.fastjson.JSON;
import com.zuo.demo01.pojo.Blog;
import com.zuo.demo01.pojo.Comment;
import com.zuo.demo01.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CommentsController {

    @Autowired
    private CommentService commentService;

    @RequestMapping("/table/list/comments")
    @ResponseBody
    public Map<String,Object> comments_list(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit){

        Map<String,Integer> map1 = new HashMap<>();
        map1.put("startIndex",(page-1)*limit);
        map1.put("pageSize",limit);

        List<Comment> list = commentService.findAll(map1);

        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",commentService.count());
        map.put("data",list);
        return map;
    }


    @RequestMapping("/comment/verify")
    @ResponseBody
    public String comment_verify(@RequestBody List<Comment> comments){
        Map<String,String> map = new HashMap<>();

        int result = 1;
        for (Comment comment:
             comments) {
            result = commentService.verify(comment.getId());
            if(result==0){
                map.put("status","error");
                break;
            }
        }
        if(result==1){
            map.put("status","success");
        }
        String remap = JSON.toJSONString(map);
        return remap;
    }


    @RequestMapping("/comment/delete")
    @ResponseBody
    public String comment_delete(@RequestBody List<Comment> comments){
        Map<String,String> map = new HashMap<>();

        int result = 1;
        for (Comment comment:
                comments) {
            result = commentService.delete(comment.getId());
            if(result==0){
                map.put("status","error");
                break;
            }
        }
        if(result==1){
            map.put("status","success");
        }
        String remap = JSON.toJSONString(map);
        return remap;
    }


}
